// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({
  env: "defend-grgsv"
})

const db = cloud.database()
const publishDB = db.collection('publishData')
const _ = db.command


// 云函数入口函数
exports.main = async (event, context) => {
  console.log("传进来的参数", event)

  try {
    if ((0 == event._academyArray.length) && (0 == event._labelArray.length)) {//只有栏数
      let totalCount = await publishDB.aggregate()
        .match({
          publishBelong: _.eq(event._col)
        })
        .lookup({
          from: 'userInfo',
          localField: 'promulgatorID',
          foreignField: '_openid',
          as: 'userinfo',
        })
        .count('totalCount').end()
      console.log("totalCount", totalCount)

      let data = await publishDB.aggregate()
        .match({
          publishBelong: _.eq(event._col)
        })
        .lookup({
          from: 'userInfo',
          localField: 'promulgatorID',
          foreignField: '_openid',
          as: 'userinfo',
        })
        .sort({
          'publishContent.date': -1,
          _id: 1
        })
        .skip((event._page - 1) * event._pageSize)
        .limit(event._pageSize)
        .end()
      var totalPage = 0
      if (totalCount.list[0]) {
        let counts = totalCount.list[0].totalCount
        totalPage = counts === 0 ? 0 : counts <= event._pageSize ? 1 : counts % event._pageSize == 0 ? parseInt(counts / event._pageSize) : parseInt(counts / event._pageSize) + 1
      } else {
        totalPage = 0
      }
      return { totalPage, data }

    } else if (0 == event._labelArray.length) {//只有栏、学院
      const totalCount = await publishDB.aggregate()
        .match({
          publishBelong: _.eq(event._col),
          publishAcademy: _.all(event._academyArray)
        })
        .lookup({
          from: 'userInfo',
          localField: 'promulgatorID',
          foreignField: '_openid',
          as: 'userinfo',
        })
        .count('totalCount').end()

      const data = await publishDB.aggregate()
        .match({
          publishBelong: _.eq(event._col),
          publishAcademy: _.all(event._academyArray)
        })
        .lookup({
          from: 'userInfo',
          localField: 'promulgatorID',
          foreignField: '_openid',
          as: 'userinfo',
        })
        .sort({
          'publishContent.date': -1,
          _id: 1
        })
        .skip((event._page - 1) * event._pageSize)
        .limit(event._pageSize)
        .end()
      var totalPage = 0
      if (totalCount.list[0]) {
        let counts = totalCount.list[0].totalCount
        totalPage = counts === 0 ? 0 : counts <= event._pageSize ? 1 : counts % event._pageSize == 0 ? parseInt(counts / event._pageSize) : parseInt(counts / event._pageSize) + 1
      } else {
        totalPage = 0
      }

      return { totalPage, data }

    } else if (0 == event._academyArray.length) {//只有栏、标签
      const totalCount = await publishDB.aggregate()
        .match({
          publishBelong: _.eq(event._col),
          publishLabel: _.all(event._labelArray)
        })
        .lookup({
          from: 'userInfo',
          localField: 'promulgatorID',
          foreignField: '_openid',
          as: 'userinfo',
        })
        .count('totalCount').end()

      const data = await publishDB.aggregate()
        .match({
          publishBelong: _.eq(event._col),
          publishLabel: _.all(event._labelArray)
        })
        .lookup({
          from: 'userInfo',
          localField: 'promulgatorID',
          foreignField: '_openid',
          as: 'userinfo',
        })
        .sort({
          'publishContent.date': -1,
          _id: 1
        })
        .skip((event._page - 1) * event._pageSize)
        .limit(event._pageSize)
        .end()

      if (totalCount.list[0]) {
        let counts = totalCount.list[0].totalCount
        totalPage = counts === 0 ? 0 : counts <= event._pageSize ? 1 : counts % event._pageSize == 0 ? parseInt(counts / event._pageSize) : parseInt(counts / event._pageSize) + 1
      } else {
        totalPage = 0
      }

      return { totalPage, data }

    } else {//有栏、学院、标签
      const totalCount = await publishDB.aggregate()
        .match({
          publishBelong: _.eq(event._col),
          publishLabel: _.all(event._labelArray),
          publishAcademy: _.all(event._academyArray)
        })
        .lookup({
          from: 'userInfo',
          localField: 'promulgatorID',
          foreignField: '_openid',
          as: 'userinfo',
        })
        .count('totalCount').end()

      const data = await publishDB.aggregate()
        .match({
          publishBelong: _.eq(event._col),
          publishLabel: _.all(event._labelArray),
          publishAcademy: _.all(event._academyArray)
        })
        .lookup({
          from: 'userInfo',
          localField: 'promulgatorID',
          foreignField: '_openid',
          as: 'userinfo',
        })
        .sort({
          'publishContent.date': -1,
          _id: 1
        })
        .skip((event._page - 1) * event._pageSize)
        .limit(event._pageSize)
        .end()
      var totalPage = 0
      if (totalCount.list[0]) {
        let counts = totalCount.list[0].totalCount
        totalPage = counts === 0 ? 0 : counts <= event._pageSize ? 1 : counts % event._pageSize == 0 ? parseInt(counts / event._pageSize) : parseInt(counts / event._pageSize) + 1
      } else {
        totalPage = 0
      }

      return { totalPage, data }

    }
  } catch (e) {
    console.error(e)
  }
}